package com.wtp.wallpaper.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtp.wallpaper.common.constant.JwtClaimsConstant;
import com.wtp.wallpaper.common.constant.MessageConstant;
import com.wtp.wallpaper.common.constant.PictureOssPathConstant;
import com.wtp.wallpaper.common.constant.UserUploadConstant;
import com.wtp.wallpaper.common.context.BaseContext;
import com.wtp.wallpaper.common.exception.PictureExpireException;
import com.wtp.wallpaper.mapper.UserUploadMapper;
import com.wtp.wallpaper.pojo.dto.UserUploadDTO;
import com.wtp.wallpaper.pojo.entity.Picture;
import com.wtp.wallpaper.pojo.entity.User;
import com.wtp.wallpaper.pojo.entity.UserUpload;
import com.wtp.wallpaper.service.*;
import com.wtp.wallpaper.utils.AliOssUtil;
import com.wtp.wallpaper.utils.PictureReduceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class UserUploadServiceImpl extends ServiceImpl<UserUploadMapper, UserUpload> implements UserUploadService {

    @Autowired
    private UserService userService;

    @Autowired
    private PictureServiceImpl pictureService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private AliOssUtil aliOssUtil;
    @Override
    public void update(UserUploadDTO userUploadDTO) throws IOException {

        UserUpload userUpload = BeanUtil.copyProperties(userUploadDTO, UserUpload.class);
        Picture p = pictureService.getById(userUpload.getId());

        if (userUploadDTO.getState() == UserUploadConstant.PASS){
            userUpload.setReason("");
            if (p != null){
                updateById(userUpload);
                throw new PictureExpireException(MessageConstant.PICTURE_EXISTS);
            }

            //userUpload
            UserUpload byId = getById(userUploadDTO.getId());

            //给通过的用户加10点积分
            userService.lambdaUpdate()
                    .setSql("coin = coin + 10")
                    .eq(User::getId, userUpload.getUserId())
                    .update();

            File fileFromOSS = aliOssUtil.getFileFromOSS(byId.getPicurl());
            pictureService.convertAndUpload(fileFromOSS);

            Picture picture = BeanUtil.copyProperties(userUpload, Picture.class);
            String s = byId.getPicurl().split("/")[1];
            picture.setPicurl(PictureOssPathConstant.PIC_URL + "/" + s);
            picture.setSmallpicurl(PictureOssPathConstant.PIC_SMALL_URL + "/" + s);
            picture.setUpdateTime(LocalDateTime.now());
            picture.setCreateTime(LocalDateTime.now());

            String nickname = BaseContext.getCurrentMap().get(JwtClaimsConstant.NICKNAME).toString();
            picture.setNickname(nickname);
            picture.setTabs(classifyService.getById(userUpload.getClassId()).getName());
            picture.setFileMd5(byId.getFileMd5());

            pictureService.save(picture);
        }else{
            if (p != null) pictureService.removePictureByIds(List.of(String.valueOf(p.getId())));
        }
        updateById(userUpload);
    }
}
